<?php 
/**
 * SwordMouse CMS
 * $Id: page.php 20 2010-08-07 16:08:46Z dan@inkping.com $
 * @package SwordMouseClases
 */
class PAGE {
	/**
	 * Array containing all page variables and settings retrieved from database.
	 * @var array
	 */
	protected $info = array();
	
	/**
	 * Fetches page variables from database and places into array for querying with PHP
	 * in page construction or editing with CMS. Checks for existence of page details in database
	 * and retrieves details if present or adds new entry.
	 */
	private function  __construct(){
		$script = $_SERVER['SCRIPT_NAME']; 
		$default = array(TYPE, HOST, PORT, BASE, CHAR, USER, PASS);
		$dbc = SITE::dbconnect($default);   
		$count = $dbc->prepare("SELECT COUNT(*) FROM page WHERE script = :script");
		$count->bindParam(':script', $script);
		$count->execute();
		if ($count->fetchColumn() > 0) {
			$query = $dbc->prepare("SELECT * FROM page WHERE script = :script");
			$query->bindParam(':script', $script);
			$query->execute();
			while($row = $query->fetch()){
				$this->info['pageid'] = $row['pageid'];
				$this->info['script'] = $row['script'];
				$this->info['charset'] = $row['charset'];
				$this->info['title'] = $row['title'];
				$this->info['description'] = $row['description'];
				$this->info['keywords'] = $row['keywords'];
				$this->info['hits'] = $row['hits'];
				$this->info['month'] = $row['month'];
				$this->info['monthhits'] = $row['monthhits'];
				$this->info['needsadmin'] = $row['needsadmin'];
			}
			// TODO increment hit values and new last viewed date, consider other metrics to measure e.g. last edited byb
		} else {
			$query = $dbc->prepare("INSERT INTO pages (pageid, script, charset, title, description, keywords, hits, month, monthhits, needsadmin) 
									VALUES (:id, :script, :charset, :title, :description, :keywords, :hits, :monthhits, :needsadmin)");
			//generate id number
			$query->bindParam(':id', $id);
			$query->bindParam(':script', $script);
			$query->bindParam(':charset', CHAR);
			$query->bindParam(':title', $_SESSION['sitename']);
			$query->bindParam(':description', $description);
			$query->bindParam(':keywords', $keywords);
			$query->bindParam(':hits', 1);
			$query->bindParam(':month', $month);
			$query->bindParam(':monthhits', 1);
			$query->bindParam(':needsadmin', 1);
			$query->execute();
		}	
	}
	
	/**
	 * Outputs all required HTML5 page <head> information and places opening <body> tag 
	 * @return string
	 */
	public function renderHtmlHead(){
		$head = "<!Doctype html>\n";
		$head .= "<html>\n";
		$head .= "<head>\n";
		$head .= $this->renderTitle()."\n";
		$head .= $this->renderDescription()."\n";
		$head .= $this->renderKeywords()."\n";
		$head .= $this->renderCharset()."\n";
		$head .= "	<!--[if lt IE 9]>\n
						<script src=\"http://html5shiv.googlecode.com/svn/trunk/html5.js\"></script>\n
					<![endif]-->
				";
		$head .= "LOAD CSS FILES HERE";
		$head .= "</head>\n";
		$head .= "<body>\n";
		return $head;
	}
	
	/**
	 * Outputs page title information in valid HTML5 format. For situations in which 
	 * rendering the full HTML head section using renderHtmlHead() is not desired.
	 * E.g. if substantial modification of the head is required.
	 * @return string
	 */
	public function renderTitle(){
		return "<title>".$this->info['title']."</title>";
	}	
	
	/**
	 * Outputs page description information in valid HTML5 format. For situations in which 
	 * rendering the full HTML head section using renderHtmlHead() is not desired.
	 * E.g. if substantial modification of the head is required.
	 * @return string
	 */
	public function renderDescription(){
		return "<meta name='description' content='$this->info['description']' />";
	}
	
	/**
	 * Outputs page k information in valid HTML5 format. For situations in which 
	 * rendering the full HTML head section using renderHtmlHead() is not desired.
	 * E.g. if substantial modification of the head is required.
	 * @return string
	 */
	public function renderKeywords(){
		return "<meta name='keywords' content='$this->info['keywords']' />";
	}
	
	/**
	 * Outputs page charset information in valid HTML5 format. For situations in which 
	 * rendering the full HTML head section using renderHtmlHead() is not desired.
	 * E.g. if substantial modification of the head is required.
	 * @return string
	 */
	public function renderCharset(){
		return "<meta http-equiv='Content-Type' content='$this->info['charset']' />";
	}
	
	/**
	 * Outputs page title information in valid HTML5 format. For situations in which 
	 * rendering the full HTML head section using renderHtmlHead() is not desired.
	 * E.g. if substantial modification of the head is required.
	 * @return string
	 */
	public function render-------(){
		return "<------  $this->info['----------']' />";
	}
	
	
	/**
	 * Outputs header property requested by function parameter in plain format without HTML
	 * @return string|int
	 * @param - property to be fetched 
	 */
	public function getPageProperty($property){
		$pageProperty = $this->info["$property"];
		return $pageProperty;
	}
	
	/**
	 * Renders requested section of page template by loading required php file.
	 * @return string
	 * @param - section to be rendered 
	 */
	public function renderSection($section){
		$html = require_once ($section.'.php');
		return $html;
	}
	
	/**
	 * Some third party javascript libraries or scripts require script tags to be inserted above the closing <body> tag. E.g. Google Analytics. 
	 * Example uses include javascript files for pre-loading site images and other content.
	 * HTML and PHP scripts can be added by may be of limited utility.
	 * For consistency and compatibility with this function Javascript should be placed in valid HTML5 tags within an HTML or PHP file which should then be placed in the html_body_end folder in the lib/includes directory.
	 * @return string
	 */
	public function getPreFooterIncludes(){
		$includes = require_once ($site::getSetting('jsdir').'/google/analyticstracking.php');
		$includes .= $this->getCustomIncludes("html_body_end");
		return $includes;
	}
	
	/**
	 * Some third party javascript libraries or scripts require script tags to be inserted within the HTML head. 
	 * Example uses include javascript files for pre-loading site images and other content. 
	 * HTML and PHP scripts can be added by may be of limited utility. 
	 * For consistency and compatibility Javascript should be placed in valid HTML5 tags within an HTML or PHP file which should then be placed in the html_head folder in the lib/includes directory.
	 * @return string
	 */
	public function getHeadIncludes(){
		//can use this to load custom css files etc $includes = require_once ($site::getSetting('jsdir').'/google/analyticstracking.php');
		$includes .= $this->getCustomIncludes("html_body_end");
		return $includes;
	}
	
	/**
	 * Only retrieves include files added to the prefooterincludeslib directory. 
	 * As a general rule getPreFooterIncludes() function should be used as this includes all the 
	 * standard/preset includes such as google analytics and CMS files.
	 * @return string
	 */
	public function getCustomIncludes($placement){
		
		//TODO check customincludeslib directory and load all included files
		for/if(//$jsInclude){
			$customIncludes .= require_once ($site::getSetting('customincludesdir').'/'.$placement.'/'..'.php');
		}
		return $customIncludes;
	}
}
?>